#!/bin/sh

pro_name=rsf
pro_ver=0.1.0-SNAPSHOT
pro_jar_name=rsf-server.jar
pro_res_name=$pro_name-$pro_ver-publish.zip
ci_host=http://dmworld002:8080
ci_job=$ci_host/h/view/GIt/job/Git-Big
jar_path=$(pwd)
test_debug_port=8888
jvm_arg="-Xms128m -Xmx1024m -XX:MaxPermSize=256m"
is_need_sync_res=true
#is_pub=false
is_pub=true
server_args="-pub=10000 -pri=0"
##
##
##
function changedir()
{
chdir=$1
if [ -d $chdir ] 
  then
  cd $chdir
 fi
}
##
##
##
function dw()
{
if [ "$is_pub" = "true" ]
then
dw_with_handson $1
fi
}
##
##
##
function dw_with_handson()
{
file=$1
echo $file

if [ -e $file ] 
then
    rm -f $file
fi
echo \n
echo starting download rsf jar file...
echo \n
wgetFile=$ci_job/ws/target/$file

while ! -e $file 
 do
  wget $wgetFile
 break
 done
wait
echo donwloading finished.
}
##
## sync all file
##
function syncRes()
{
  changedir tmp
  if [ -d res ];
  then
   echo delete the traget folder,if it is exist.
     rm -rf res
  fi

  dw $pro_res_name
  wait
  echo unzip the source staff.
  unzip $pro_res_name -d res
  wait
  echo starting reload the websit contents.
  echo clean the old files.
  cd ..

  if [ -d pages ] ;
  then
  echo clear up the pages folder.
  rm pages/* -rf
  else
  echo create the pages folder.
  mkdir pages
  fi

  pwd 
 echo mv the new pages to websit for deploy.
  mv ./tmp/res/pages/* ./pages
 echo cp all page for debug env.
 
  if [ ! -d ./src/main/resource ];
 then
 mkdir -p ./src/main/resource
fi
}

 if [ -d ./src/main/resource ];
 then
 mv -u ./pages ./src/main/resources
fi
}

##
## sync runable jar file 
##
function syncCI()
{
changedir tmp
dw $pro_jar_name
srcmd5=( $(md5sum $pro_jar_name|awk '{print $1}') )
targetmd5=( $(md5sum ../$pro_jar_name|awk '{print $1}') )
echo srcmd5
if [ "$srcmd5"x != "$targetmd5"x ] ;
then
  echo dowonload file md5 value: $srcmd5 different with source file md5 value: $targetmd5, SO override the target jar with source file.
  cp $pro_jar_name ../ -R
else
 echo there are same md5 value $srcmd5. kill downlowd file.
 rm -f $pro_jar_name
fi
echo go back folder.
cd ..
}
function start()
{
  if [ "$is_need_sync_res"="true" ];
  then
   syncCI
   syncRes
#   changedir ../
  fi
  repMix
  echo starting....
  echo the jvm argurments is $jvm_arg
  java $jvm_arg -jar rsf-server.jar $server_args &
  echo start finish.
}
function stop()
{
  echo stop....
  param=java
  ps aux |grep "java"|grep -v "grep"| grep -v "rsf.sh"| awk '{print $2}'|xargs -i kill -9 {}
  echo stop finish.
}

function debug()
{
  echo debug starting....
  if [ "$is_need_sync_res"="true" ];
    then
       syncCI
       changedir ../
    fi

 if [ "$test_debug_port"a != ""a ];
 then
  debug_port=$test_debug_port
 else
  debug_port=8888
 fi
 
 java $jvm_arg -Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=$debug_port,server=y,suspend=y -jar rsf-server.jar &
  echo starting finish.
}
##
## bak the runntime jar package and pages
##
bak_prefix=bak_
function backup()
{ 
  if [ ! -d bak ];
  then 
  mkdir bak
  fi
  changedir bak

 # bak_prefix=bak_
  postfix=$(date +%m%d%H%M)
  bakFileName=$bak_prefix$postfix.tar.gz
  if [ -f $bakFileName ];
  then
    echo $(date +%F %T) -- already delete the bak file : $bakFileName.
    rm -rf $bakFileName
  fi
tar -zcvf $bakFileName ../$pro_jar_name ../pages ../rsf.sh
#   touch $bakFileName
echo $bakFileName
}
##
##
##
function console()
{
remote_port=8384
remote_pwd=/opt/rsf/bak/rsf.console.pwd
remote_auth=false
jvm_arg="$jvm_arg -Dcom.sun.management.jmxremote.port=$remote_port -Dcom.sun.management.jmxremote.password.file=$remote_pwd -Dcom.sun.management.jmxremote.authenticate=$remote_auth -Dcom.sun.management.jmxremote.ssl=false  "
echo $jvm_arg
#restart server with new jvm arguments.
stop
start
}
##
##
##
function repMix()
{
comp_mix=d
echo =========
echo "find all sea.js file, and find the require parameter."
find . -name "sea.js" -print| xargs grep "require"
echo =========
echo "replace all require to specify parameter name."
find . -name "sea.js" -print| xargs perl -pi.bak -e "s/require/$comp_mix/g"
find . -name "sea.js" -print| xargs grep "require"
echo =========
echo "shouldn't be find any record."

}
##
##
##
function main()
{
 case "$2" in
 start)
  start
 ;;
 stop)
 stop 
;;
 restart)
stop
start 
;;
 debug)
 debug
;;
redebug)
stop
debug
;;
 sync)
syncRes
syncCI
repMix
;;
 status)
 ps aux |grep "java"|grep -v "grep"| grep -v "$1"|awk '{if("$2"a!=""a) {print "server was running the sid : "$2} else {print "Server already stop."}}'
 echo status: 
 ;;
 bak)
  backup
;;
 console)
 console
;;
rep)
repMix
;;
 *)
 echo "Usage: $1 {start|stop|restart|status|debug|redebug|sync|bak|console }"
 exit 1 
 ;;
esac

}
main $0 $1
